package array

import (
	"sort"
)

func merge(intervals [][]int) [][]int {
	sort.Slice(intervals, func(i, j int) bool {
		return intervals[i][0] < intervals[j][0]
	})

	res := [][]int{}
	min, max := intervals[0][0], intervals[0][1]

	for _, l := range intervals {
		if l[0] > max {
			res = append(res, []int{min, max})
			min = l[0]
			max = l[1]
			continue
		}

		if l[1] > max {
			max = l[1]
		}
	}
	res = append(res, []int{min, max})
	return res

}
